package com.shanghaiyimu.yimiyangche.fragment;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.activity.DingDanBaoYangActivity;
import com.shanghaiyimu.yimiyangche.activity.SelectShopToAnZhuangActivity;
import com.shanghaiyimu.yimiyangche.widget.SelcetViewWithText;

import static com.shanghaiyimu.yimiyangche.R.id.shoucang_tv;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsFragment extends Fragment implements View.OnClickListener{



    public GoodsFragment() {
        // Required empty public constructor
    }
    private PopupWindow popupWindow;
    private TextView select;
    private ImageView dismiss;
    private View   popupView;
    private SelcetViewWithText yanse,chicun,guige;
    private String[] mLabels1 = {"红色","蓝色","银色","黑色"};
    private String[] mLabels2 = {"XL","XXL","XXXL"};
    private String[] mLabels3 = {"4W-50","5W-60","3W-40","5W-50"};
    private TextView add,reduce,num,queding,goods_content,queding2_pop,lijigoumai,jiarugouwuche,shoucang,mendian_tw;
    private LinearLayout linearLayout;
    private boolean isClick = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_goods, container, false);
        initView(view);
        return view;
    }

    public PopupWindow getPopWindow(){
        return popupWindow;
    }

    private void initView(View view) {
        shoucang = (TextView) view.findViewById(shoucang_tv);
        goods_content = (TextView) view.findViewById(R.id.goods_content_select_text);
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popwindom_shop_select, null);

        dismiss = (ImageView) popupView.findViewById(R.id.pop_dismiss);
        dismiss.setOnClickListener(this);

        yanse = (SelcetViewWithText) popupView.findViewById(R.id.yasnetianjia_selcetview);
        chicun = (SelcetViewWithText) popupView.findViewById(R.id.chicuntiajia_selectview);
        guige = (SelcetViewWithText) popupView.findViewById(R.id.xinghaotianjia_selectview);


        initLabel(mLabels1,yanse);
        initLabel(mLabels2,chicun);
        initLabel(mLabels3,guige);

        add = (TextView) popupView.findViewById(R.id.add);
        reduce = (TextView) popupView.findViewById(R.id.reduce);
        num = (TextView) popupView.findViewById(R.id.num);
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);

        queding2_pop = (TextView) popupView.findViewById(R.id.queding2_pop);
        queding = (TextView) popupView.findViewById(R.id.queding_pop);
        linearLayout = (LinearLayout) popupView.findViewById(R.id.xuanzeshitanchu_pop);
        lijigoumai = (TextView) popupView.findViewById(R.id.lijigoumai_pop);
        jiarugouwuche = (TextView) popupView.findViewById(R.id.jiarugouwuche_pop);

        mendian_tw = (TextView) popupView.findViewById(R.id.mendian_tw);
        queding.setOnClickListener(this);
        queding2_pop.setOnClickListener(this);
        lijigoumai.setOnClickListener(this);
        jiarugouwuche.setOnClickListener(this);
        shoucang.setOnClickListener(this);
        mendian_tw.setOnClickListener(this);

        /**
         * contentView:控制popupWindow显示的样式
         * width: popupWindow的宽  LayoutParams  android.view.viewgroup
         * height: popupWindow的高
         */
        popupWindow = new PopupWindow(popupView,ActionBar.LayoutParams.MATCH_PARENT,1000);
        //防止无法关闭popupWindow
        popupWindow.setFocusable(true);
        //点击popupWindow外部会不会自动关闭
        popupWindow.setOutsideTouchable(true);
//        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        //如果要放置背景颜色#FFD700
        ColorDrawable colorDrawable = new ColorDrawable(0x00D700);
        popupWindow.setBackgroundDrawable(colorDrawable); //设置背景显示图片


        select = (TextView) view.findViewById(R.id.goods_content_select_text);
        select.setOnClickListener(this);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (getActivity() != null) {
                    setBackgroundAlpha(getActivity() , 1f);
                }
            }
        });

    }

    public void showPop(int i,int j,int k){

        popupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 0);
        if (i  == 1) {
            queding.setVisibility(View.VISIBLE);
            queding2_pop.setVisibility(View.INVISIBLE);
            linearLayout.setVisibility(View.INVISIBLE);
        }
        if (j == 1){
            queding.setVisibility(View.INVISIBLE);
            queding2_pop.setVisibility(View.VISIBLE);
            linearLayout.setVisibility(View.INVISIBLE);
        }
        if (k == 1){
            queding.setVisibility(View.INVISIBLE);
            queding2_pop.setVisibility(View.INVISIBLE);
            linearLayout.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void onClick(View v) {
        String sum = num.getText().toString();
        int i = Integer.parseInt(sum);
        switch (v.getId()){
            case R.id.goods_content_select_text:

                showPop(0,0,1);

                setBackgroundAlpha(getActivity(),0.5f);

                break;
            case R.id.pop_dismiss:
                popupWindow.dismiss();
                break;
            case R.id.add:

                if (i == 8){
                    Toast.makeText(getContext(),i+"超出购买限制",Toast.LENGTH_SHORT).show();
                }else {
                    i = i+1;
                    num.setText(i+"");
                }
                break;
            case R.id.reduce:
                if (i == 1){
                    Toast.makeText(getContext(),"最低购买为"+i,Toast.LENGTH_SHORT).show();
                }else {
                    i = i - 1;
                    num.setText(i+"");
                }
                break;
            case R.id.queding_pop:
                String xuqiu = "已选："+num.getText()+"L"+getSelectText(yanse)+","+getSelectText(chicun)+","+"规格";
                goods_content.setText(xuqiu);
                popupWindow.dismiss();
                startActivity(new Intent(getContext(),DingDanBaoYangActivity.class));
                break;
            case R.id.queding2_pop:
                String xuqiu2 = "已选："+num.getText()+"L"+getSelectText(yanse)+","+getSelectText(chicun)+","+"规格";
                goods_content.setText(xuqiu2);
                popupWindow.dismiss();
                Toast.makeText(getContext(),"加入购物车",Toast.LENGTH_SHORT).show();
                break;
            case R.id.jiarugouwuche_pop:
                String xuqiu3 = "已选："+num.getText()+"L"+getSelectText(yanse)+","+getSelectText(chicun)+","+"规格";
                goods_content.setText(xuqiu3);
                Toast.makeText(getContext(),"加入成功",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                break;
            case R.id.lijigoumai_pop:
                String xuqiu4 = "已选："+num.getText()+"L"+getSelectText(yanse)+","+getSelectText(chicun)+","+"规格";
                goods_content.setText(xuqiu4);
                Toast.makeText(getContext(),"购买成功" +xuqiu4,Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getContext(),DingDanBaoYangActivity.class));
                popupWindow.dismiss();
                break;
            case shoucang_tv:
                if (!isClick) {
                    shoucang.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.star_on), null, null);
                    isClick = true;
                    Toast.makeText(getContext(),"收藏成功",Toast.LENGTH_SHORT).show();
                }else {
                    shoucang.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.star_off), null, null);
                    isClick = false;
                    Toast.makeText(getContext(),"取消收藏",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mendian_tw:
                Intent intent = new Intent(getContext(),SelectShopToAnZhuangActivity.class);
                startActivityForResult(intent,0);
                break;
            default:
                break;
        }
    }
    /**
     * 设置页面的透明度
     * @param bgAlpha 1表示不透明
     */
    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }



    // 初始化标签
    public void initLabel(final String[] mLabels, final SelcetViewWithText mFlowLayout) {
        int weidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(weidth,height);
        layoutParams.setMargins(20, 20, 7, 7);// 设置边距
        for (int i = 0; i < mLabels.length; i++) {
            final TextView textView = new TextView(getContext());
            textView.setTag(i);
            textView.setTextSize(12);
            textView.setText(mLabels[i]);
            textView.setPadding(16, 8, 16, 8);
            textView.setTextColor(Color.BLACK);
            textView.setBackgroundResource(R.drawable.lable_item_bg_normal);
            mFlowLayout.addView(textView, layoutParams);
            // 标签点击事件
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clearSet(mFlowLayout);
                    int i = Integer.parseInt(String.valueOf(textView.getTag()));
                    textView.setBackgroundResource(R.drawable.lable_item_bg_unormal);
                    textView.setTextColor(Color.RED);
                    v.setEnabled(false);
//                    Toast.makeText(getContext().getApplicationContext(), mLabels[i], Toast.LENGTH_SHORT).show();
                }
            });

        }

    }
    public void clearSet(SelcetViewWithText mFlowLayout){

        int childCount = mFlowLayout.getChildCount();
        for (int i = 0; i <  childCount; i++) {
            TextView childAt = (TextView) mFlowLayout.getChildAt(i);
            childAt.setEnabled(true);
            String s = childAt.getText().toString();
            childAt.setTextColor(Color.BLACK);
            childAt.setBackgroundResource(R.drawable.lable_item_bg_normal);
        }
    }
    public String getSelectText(SelcetViewWithText mFlowLayout){
        int childCount = mFlowLayout.getChildCount();
        for (int i = 0; i <  childCount; i++) {
            TextView childAt = (TextView) mFlowLayout.getChildAt(i);
            if (!childAt.isEnabled()){
                return childAt.getText().toString();
            }
        }
        return "";

    }
//    private String name = "   请选择门店";
//    public void setDianPu(String name){
//        this.name = name;
//        mendian_tw.setText(name);
//    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 0:
//                if (!mendian_tw.getText().equals("    请选择门店")){
//                }
                String name = data.getExtras().getString("name");
                if (name != null) {
                    mendian_tw.setText("  "+name);
                }else {
                   break;
                }
                break;

            default:
                break;
            }
        }
    }

